package com.cashmama.market.api.domain.service;

import com.cashmama.market.api.domain.enums.AlgoStatusEnum;
import com.cashmama.market.api.domain.handler.Algo360Handler;
import com.cashmama.market.api.infras.config.ApolloConfigUtil;
import com.cashmama.market.api.infras.enums.error.UserProfileErrorCodeEnum;
import com.cashmama.market.api.infras.exception.ApplicationException;
import com.cashmama.market.api.infras.utils.StringUtils;
import com.fqgj.common.api.Response;
import com.fqgj.xjd.user.client.UserService;
import com.fqgj.xjd.user.client.response.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * @program: market-api
 * @description algo360 sms validate
 * @author: frank
 * @create: 2020-01-08 10:50
 **/
@Service
public class NotifyService {

    private static final Logger LOGGER = LoggerFactory.getLogger(NotifyService.class);
    @Autowired
    private Algo360Handler algo360Handler;
    @Autowired
    private UserService userService;
    @Autowired
    private ApolloConfigUtil apolloConfigUtil;

    public void notifyAlgo360(HttpServletRequest request){
        String userId = request.getParameter("user_id");
        String algoStatus = request.getParameter("algo_status");
        LOGGER.info("notify algo360 userId:{}, algoStatus:{}", userId, algoStatus);
        if(StringUtils.isEmpty(userId)){
            return;
        }
        if(!apolloConfigUtil.needALGO360Verify()){
            return;
        }
        Response<User> userByUserCode = userService.getUserByUserCode(userId);
        if(userByUserCode.getData() == null){
            throw new ApplicationException(UserProfileErrorCodeEnum.USER_PROFILE_NO_EXIST);
        }
        if(Objects.equals(algoStatus, AlgoStatusEnum.ALGO360_PROCESS_FINISHED.getType())){
            algo360Handler.getVariablesData(userId);
        }else if(Objects.equals(algoStatus, AlgoStatusEnum.ALGO360_SCORE_CALCULATED.getType())){
            algo360Handler.getVariableScore(userId);
        }
    }

}
